using System;
using System.Runtime.Serialization;
using SharpObjects.Logging.Utils;

namespace SharpObjects.Logging
{


    [Serializable]
	public class LogEntry : ILogEntry
    {
        protected static string hostName;

		protected DateTime time;

		protected string category;
		protected string origin;
		protected string message;

		protected string machineName;
		protected int threadId;
		protected string threadName;

		protected string processName;
		protected int processId;

		protected string stackTrace;

		public LogEntry()
		{
			this.processName = null;
			this.processId = -1;
			this.stackTrace = null;
		}
		
		public LogEntry(bool bInitWithCommonInfo)
		{
			if(bInitWithCommonInfo)
			{
				this.processName = ProcessInformation.ProcessName;
				this.processId = ProcessInformation.ProcessId;
				this.machineName = hostName;
				this.threadId = AppDomain.GetCurrentThreadId();
				this.threadName = System.Threading.Thread.CurrentThread.Name; 
			}
			else
			{
				this.processName = string.Empty;
				this.processId = -1;
				this.machineName = null;
				this.threadId = -1;
				this.threadName = null;
			}

			this.time = DateTime.Now;
			this.category = null;
			this.origin = null;
			this.message = null;
			this.stackTrace = null;
		}
		
		public LogEntry(string message, string stackTrace, string origin, string category, bool bInitWithCommonInfo)
		{
			if(bInitWithCommonInfo)
			{
				this.processName = ProcessInformation.ProcessName;
				this.processId = ProcessInformation.ProcessId;
				this.machineName = hostName;
				this.threadId = AppDomain.GetCurrentThreadId();
				this.threadName = System.Threading.Thread.CurrentThread.Name; 
			}
			else
			{
				this.processName = string.Empty;
				this.processId = -1;
				this.machineName = null;
				this.threadId = -1;
				this.threadName = null;
			}

			this.time = DateTime.Now;
			this.category = category;
			this.origin = origin;
			this.message = message;
			this.stackTrace = stackTrace;
		}
		
		public DateTime Time { get { return this.time; } set { this.time = value; } }
		public string Category { get { return this.category; } set { this.category = value; } }
		public string Origin { get { return this.origin; } set { this.origin = value; } }
		public string MachineName { get { return this.machineName; } set { this.machineName = value; } }
		public string ProcessName { get { return this.processName; } set { this.processName = value; } }
		public int ProcessId { get { return this.processId; } set { this.processId = value; } }
		public int ThreadId { get { return this.threadId; } set { this.threadId = value; } }
		public string ThreadName { get { return this.threadName; } set { this.threadName = value; } }
		public string Message { get { return this.message; } set { this.message = value; } }
		public string StackTrace { get { return this.stackTrace; } set { this.stackTrace = value; } }

		public string ThreadTag
		{
			get
			{
				if(ThreadName == null || ThreadName.Trim() == string.Empty)
				{
					return "Thread ID: " + ThreadId.ToString();
				}
				else
				{
					return string.Format("Thread ID: {0}, Thread Name: {1}", ThreadId, ThreadName);
				}
			}
		}


		static LogEntry()
		{
			hostName = System.Net.Dns.GetHostName();
		}

		//#region ISerializable Members

		public LogEntry(SerializationInfo info, StreamingContext context)
		{
			this.time = info.GetDateTime("time");
			this.category = info.GetString("category");
			this.origin = info.GetString("origin");
			this.machineName = info.GetString("machineName");
			this.processName = info.GetString("processName");
			this.processId = info.GetInt32("processId");
			this.threadId = info.GetInt32("threadId");
			this.threadName = info.GetString("threadName");
			this.message = info.GetString("message");
			this.stackTrace = info.GetString("stackTrace");
		}

		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("time", this.time);
			info.AddValue("category", this.category);
			info.AddValue("origin", this.origin);
			info.AddValue("machineName", this.machineName);
			info.AddValue("processName", this.processName);
			info.AddValue("processId", this.processId);
			info.AddValue("threadId", this.threadId);
			info.AddValue("threadName", this.threadName);
			info.AddValue("message", this.message);
			info.AddValue("stackTrace", this.stackTrace);
		}

		//#endregion

		
		
		
		
		
		
		
		
		
		
		

	}
}
